package com.woniu.controller;

import com.woniu.service.DashboardService;
import com.woniu.utils.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/api/dashboard")
@CrossOrigin
public class DashboardController {

    @Autowired
    private DashboardService dashboardService;

    // 总销售额
    @GetMapping("/totalSales")
    public ResultObj getTotalSales() {
        try {
            BigDecimal totalSales = dashboardService.getTotalSales();
            return ResultObj.ok().data( totalSales);
        } catch (Exception e) {
            return ResultObj.error().msg("获取总销售额失败");
        }
    }

    // 订单总数
    @GetMapping("/orderCount")
    public ResultObj getOrderCount() {
        try {
            Long orderCount = dashboardService.getOrderCount();
            return ResultObj.ok().data(orderCount);
        } catch (Exception e) {
            return ResultObj.error().msg("获取已完成订单总数失败");
        }
    }

    // 待支付订单总数
    @GetMapping("/getPendingOrderCount")
    public ResultObj getPendingOrderCount() {
        try {
            Long orderCount = dashboardService.getPendingOrderCount();
            return ResultObj.ok().data(orderCount);
        } catch (Exception e) {
            return ResultObj.error().msg("获取待支付订单总数失败");
        }
    }

    // 支付用户数
    @GetMapping("/payingUserCount")
    public ResultObj getPayingUserCount() {
        try {
            Long userCount = dashboardService.getPayingUserCount();
            return ResultObj.ok().data( userCount);
        } catch (Exception e) {
            return ResultObj.error().msg("获取支付用户数失败");
        }
    }

    // 统计待审核教师数量
    @GetMapping("/pendingTeachers")
    public ResultObj countPendingTeachers() {
        try {
            Long count = dashboardService.countPendingTeachers();
            return ResultObj.ok().data(count);
        } catch (Exception e) {
            return ResultObj.error().msg("统计待审核教师失败");
        }
    }

    // 统计新增用户数量
    @GetMapping("/newUsers")
    public ResultObj countNewUsers() {
        try {
            Long count = dashboardService.countNewUsers();
            return ResultObj.ok().data(count);
        } catch (Exception e) {
            return ResultObj.error().msg("统计新增用户失败");
        }
    }

    @GetMapping("/yesterdayPaymentTrend")
    public ResultObj getYesterdayPaymentTrend() {
        try {
            List<Map<String, Object>> trendData = dashboardService.getYesterdayPaymentTrend();
            // 处理为24小时完整数据
            Map<Integer, Double> resultMap = new HashMap<>();
            for (int i = 0; i < 24; i += 2) {
                resultMap.put(i, 0.0);
            }

            trendData.forEach(item -> {
                Integer hour = ((Number) item.get("hour_interval")).intValue();
                BigDecimal amount = (BigDecimal) item.get("total_amount");
                resultMap.put(hour, amount != null ? amount.doubleValue() : 0.0);
            });

            return ResultObj.ok().data(resultMap);
        } catch (Exception e) {
            return ResultObj.error().msg("获取昨日支付趋势失败");
        }
    }
}
